package com.yuneec.fly.utils;

import android.app.Fragment;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by peng.gao on 2016/8/24.
 */
public class PreventMistakenlyTouchHelper {
    /**
     * @param motionEvent
     * @param preventView 误触到这个view时则当前view的触摸事件被打断
     * @return
     */
    public static boolean onInterceptTouchEvent(MotionEvent motionEvent, View preventView) {
        if (preventView != null) {
            int[] viewLocation = new int[2];
            preventView.getLocationOnScreen(viewLocation);
            float x = viewLocation[0];
            float y = viewLocation[1];
            float width = preventView.getWidth();
            float height = preventView.getHeight();
            float majorAxis = motionEvent.getTouchMajor();
            majorAxis /= 2;
            float minorAxis = motionEvent.getTouchMinor();
            minorAxis /= 2;
            float touchX = motionEvent.getRawX();
            float touchY = motionEvent.getRawY();
            float top = y - minorAxis;
            float bottom = y + height + minorAxis;
            float left = x - majorAxis;
            float right = x + width + majorAxis;
            if (touchX > left && touchX < right && touchY > top && touchY < bottom) {
                return true;
            }
        }
        return false;
    }

    public static boolean onInterceptTouchEvent(MotionEvent motionEvent, Fragment preventFragment) {
        if (preventFragment != null) {
            onInterceptTouchEvent(motionEvent, preventFragment.getView());
        }
        return false;
    }
}
